package leetcode;

/**
 * 描述:24. 两两交换链表中的节点
 *
 * @author Madison You
 * @created 1:19
 */
public class Mid_24_SwapPairs {
    public static void main(String[] args) {
        int[] ints = {1,2,3,4};
        ListNode node = Mid_2_AddTwoNumbers.createNode(ints);
        new Mid_24_SwapPairs().swapPairs(node);
    }

    public ListNode swapPairs(ListNode head) {
        ListNode dynamic = new ListNode(0);
        dynamic.next = head;
        ListNode temp = dynamic;
        while (temp.next != null && temp.next.next != null) {
            ListNode next1 = temp.next;
            ListNode next2 = temp.next.next;
            temp.next = next2;
            next1.next = next2.next;
            next2.next = next1;
            temp = next1;
        }
        return dynamic.next;
    }

}
